//
//  PictureNewsController.swift
//  网易新闻
//
//  Created by wl on 15/11/20.
//  Copyright © 2015年 wl. All rights reserved.
//  图片新闻界面控制器
/***************************************************
*  如果您发现任何BUG,或者有更好的建议或者意见，请您的指教。
*邮箱:wxl19950606@163.com.感谢您的支持
***************************************************/
import UIKit
// TODO: 最后一页推荐页使用cyclePictureView是做不了的，暂时先这样
class PictureNewsController: UIViewController , CyclePictureViewDelegate {
   
//========================================================
// MARK: - 一些属性
//========================================================
    /// 顶部类似导航栏的视图
    @IBOutlet weak var topView: UIView!
    /// 底部类似于tableBar的视图
    @IBOutlet weak var bottomView: UIView!
    /// 新闻标题的容器
    @IBOutlet weak var titleContainerView: UIView!
    
    /// 跟帖按钮，点击进入跟帖界面
    @IBOutlet weak var replyCountBtn: UIButton!
    /// 图片查看器
    @IBOutlet weak var cyclePictureView: CyclePictureView!
    /// 标题
    @IBOutlet weak var titleLabel: UILabel!
    /// 页数
    @IBOutlet weak var pageLabel: UILabel!
    /// 图片的描述
    @IBOutlet weak var NewsTextView: UITextView!
    @IBOutlet weak var downloadBtn: UIButton!
    var newsModel: NewsModel?
    var newsPictureModel: [userPersonPhoto]? {
        didSet {
            self.setupSubview()
        }
    }
//========================================================
// MARK: - 一些方法
//========================================================
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBarController?.tabBar.hidden = true
        
        //assert(self.newsModel?.photosetID != nil)
        // 加载本地缓存数据
//        if let newsPictureModel = LocalDataTool.getNewsDetailInfo(self.newsModel!.photosetID!) as? NewsPictureModel {
//            self.newsPictureModel = newsPictureModel
//        }
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    
    func setupSubview() {
        
//        guard let newsPictureModel = self.newsPictureModel else {
//            return
//        }
        self.cyclePictureView.autoScroll = false
        
        self.cyclePictureView.needEndlessScroll = false
        self.cyclePictureView.showPageControl = false
       // self.cyclePictureView.delegate = self
        self.cyclePictureView.placeholderImage = UIImage(named: "placeholder")
        
        self.cyclePictureView.imageURLArray =  {
            var array: [String] = []
            for index in 0...newsPictureModel!.count {
                array.append(newsPictureModel![index].imgsrc)
            }
            return array
        }()
        
        self.cyclePictureView.pictureContentMode = .ScaleAspectFit
        //self.titleLabel.text = newsPictureModel.setname
       // self.pageLabel.text = "1/\(newsPictureModel.imgsum)"
        //self.NewsTextView.text = newsPictureModel.photos[0].note
        
    }
    
    @IBAction func backBtnClik() {
        self.navigationController?.popViewControllerAnimated(true)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        guard let newsPictureModel = self.newsPictureModel else {
            return
        }
        let vc = segue.destinationViewController as! CommentViewController
        
        //vc.requestID =  newsPictureModel.postid
    }
    
    // MARK: - cyclePictureView代理
    func cyclePictureView(cyclePictureView: CyclePictureView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        UIView.animateWithDuration(0.5) { () -> Void in
            self.topView.alpha = 1 - self.topView.alpha
            self.titleContainerView.alpha = 1 - self.titleContainerView.alpha
            self.NewsTextView.alpha = 1 - self.NewsTextView.alpha
             self.bottomView.alpha = 1 -  self.bottomView.alpha
        }
    }
    
    /**
    这个协议方法是自己修改源码添加的，原本是没有的
    */
    func cyclePictureViewDidEndDecelerating(cyclePictureView: CyclePictureView) {
        guard let newsPictureModel = self.newsPictureModel else {
            return
        }
        let index = cyclePictureView.getCurrentCellIndex()
        //self.pageLabel.text = "\(index+1)/\(newsPictureModel.imgsum)"
        //self.NewsTextView.text = newsPictureModel.photos[index].note
    }


    
    @IBAction func downloadBtnAction(sender: UIButton){
        print("xiazai")
        
//         UIImageWriteToSavedPhotosAlbum(iconView.image, self, "image:didFinishSavingWithError:contextInfo: ", nil)
        
//        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
//                  var ctx = UIGraphicsGetCurrentContext()
//                  layer.renderInContext(ctx)
//                  let img = UIGraphicsGetImageFromCurrentImageContext()
//                 UIGraphicsEndImageContext()
//                 UIImageWriteToSavedPhotosAlbum(img, self, "image:didFinishSavingWithError:contextInfo:", nil)
    }
    
      func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>){
          //        if let r = error {
          //            print(r)
          //        }else{
          //        let e:NSError? = error
                  if let ee = error as NSError? {
                          print(ee)
                      }else{
                          UIAlertView(title:nil, message: "保存成功！", delegate: nil, cancelButtonTitle: "确定").show()
                      }
              }
}
// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com